package org.sea.mybator.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;

public class ChangeCharset {
    public static final String US_ASCII = "US-ASCII";
    public static final String UNICODE = "Unicode";
    public static final String ISO_8859_1 = "ISO-8859-1";
    public static final String UTF_8 = "UTF-8";
    public static final String UTF_16BE = "UTF-16BE";
    public static final String UTF_16LE = "UTF-16LE";
    public static final String UTF_16 = "UTF-16";
    public static final String GBK = "GBK";

    public static String toASCII(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "US-ASCII");
    }

    public static String toISO_8859_1(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "ISO-8859-1");
    }

    public static String toUTF_8(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "UTF-8");
    }

    public static String toUTF_16BE(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "UTF-16BE");
    }

    public static String toUTF_16LE(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "UTF-16LE");
    }

    public static String toUTF_16(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "UTF-16");
    }

    public static String toGBK(String str) throws UnsupportedEncodingException {
        return changeCharset(str, "GBK");
    }

    public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            byte[] bs = str.getBytes();

            return new String(bs, newCharset);
        }
        return null;
    }

    public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            byte[] bs = str.getBytes(oldCharset);

            return new String(bs, newCharset);
        }
        return null;
    }

    

    public static String codeString(String fileName) throws Exception {
        @SuppressWarnings("resource")
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        String code = null;

        switch (p) {
            case 61371:
                code = "UTF-8";
                break;
            case 65534:
                code = "Unicode";
                break;
            case 65279:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }

        return code;
    }
    
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "This is a 中文的 String!";
        System.out.println("str: " + str);
        String gbk = toGBK(str);
        System.out.println("转换成GBK码: " + gbk);
        System.out.println();
        String ascii = toASCII(str);
        System.out.println("转换成US-ASCII码: " + ascii);
        gbk = changeCharset(ascii, "US-ASCII", "GBK");
        System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk);
        System.out.println();
        String iso88591 = toISO_8859_1(str);
        System.out.println("转换成ISO-8859-1码: " + iso88591);
        gbk = changeCharset(iso88591, "ISO-8859-1", "GBK");
        System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk);
        System.out.println();
        String utf8 = toUTF_8(str);
        System.out.println("转换成UTF-8码: " + utf8);
        gbk = changeCharset(utf8, "UTF-8", "GBK");
        System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk);
        System.out.println();
        String utf16be = toUTF_16BE(str);
        System.out.println("转换成UTF-16BE码:" + utf16be);
        gbk = changeCharset(utf16be, "UTF-16BE", "GBK");
        System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk);
    }
}
